<template>
  <div class="comment-item" :style="{ marginLeft: depth * 30 + 'px' }">
    <div class="comment-header">
      <div class="author-info">
        <!-- 添加头像显示 -->
        <img :src="'/dev-api'+comment.author.avatar" alt="头像" class="author-avatar">
        <span class="author-name">{{ comment.author.nickName }}</span>
        <template v-if="parentAuthor">
          回复
          <span class="reply-target">{{ parentAuthor.nickName }}</span>
        </template>
      </div>
      <div class="actions">
        <el-button
          size="mini"
          type="text"
          @click="handleReply"
        >回复</el-button>
        <button class="delete-button" @click="handleDelete(comment.commentId)">删除</button>
      </div>
    </div>
    <div class="comment-content" v-html="comment.content"></div>
    <div class="comment-time">{{ formatTime(comment.createAt) }}</div>
    <template v-if="comment.children">
      <CommentItem
        v-for="child in comment.children"
        :key="child.commentId"
        :comment="child"
        :parent-author="comment.author"
        :depth="depth + 1"
        @on-delete-success="$emit('on-delete-success')"
        @reply="handleChildReply"
      />
    </template>
  </div>
</template>

<script>
import dayjs from 'dayjs';
import {delComments} from "@/api/know/comments";

export default {
  name: 'CommentItem',
  props: ['comment', 'parentAuthor', 'depth'],
  methods: {
    handleChildReply(commentId, userName,commentType) {
      this.$emit('reply', commentId, userName,commentType)
    },
    handleReply() {
      this.$emit('reply', this.comment.commentId, this.comment.author.nickName,this.comment.commentType);
    },
    formatTime(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm');
    },
    handleDelete(commentId) {
      delComments(commentId).then(response => {
        try {
          if(response.code===200){
            this.$message.success('评论删除成功');
            this.$emit('on-delete-success');  // 事件触发
          }else {
            this.$message.warning('该评论存在子评论，不能删除');
          }
        }catch (e){
          this.$message.warning('该评论存在子评论，不能删除');
        }

      });
    },
  },
};
</script>
<style scoped>
.comment-item {
  margin: 12px 0;
  padding: 16px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
  transition: all 0.3s ease;
}

.comment-item:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  transform: translateY(-2px);
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.author-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.author-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.author-name {
  color: #1a1a1a;
  font-weight: 600;
  font-size: 14px;
}

.reply-target {
  color: #666;
  font-size: 13px;
  font-weight: normal;
  margin-left: 6px;
}

.comment-content {
  margin: 12px 0;
  color: #333;
  line-height: 1.6;
  font-size: 14px;
  padding-left: 46px; /* 对齐头像 */
}

.comment-content ::v-deep img {
  max-width: 100%;
  border-radius: 4px;
  margin: 8px 0;
}

.comment-time {
  color: #999;
  font-size: 12px;
  padding-left: 46px;
}

.delete-button {
  background: none;
  border: none;
  color: #f56c6c;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  transition: all 0.2s;
}

.delete-button:hover {
  background: #fef0f0;
}

/* 层级调整 */
.comment-item .comment-item {
  margin-left: 46px !important; /* 头像宽度 + 间距 */
  position: relative;
  background: #f8f9fa;
}

.comment-item .comment-item::before {
  content: "";
  position: absolute;
  left: -23px;
  top: 24px;
  bottom: 24px;
  width: 2px;
  background: #e5e7eb;
}

.comment-item .comment-item .comment-item {
  margin-left: 30px !important;
  background: #fff;
}

.comment-item .comment-item .comment-item::before {
  left: -23px;
  background: #e5e7eb;
}
/* 在原有样式基础上添加 */
.actions {
  display: flex;
  gap: 8px;
  margin-left: auto; /* 让操作按钮靠右 */
}
</style>
